package code.structure.manager.loader
{
	import com.loadPackage.loadingtypes.ImageItem;
	import com.loadPackage.loadingtypes.LoadingItem;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.Dictionary;

	/**
	 *图片解析器 
	 * @author Yuri
	 * 
	 */
	public class ImageLoaderAnalyer implements IAnalyer
	{
		private var _classDic:Dictionary;
		private var _callBackDic:Dictionary;
		public function ImageLoaderAnalyer()
		{
			_classDic = new Dictionary();
			_callBackDic = new Dictionary()
		}
		
		public function getData(key:String,subKey:String,callBackFun:Function=null):*{
//			var classkey:String = key;
			var classkey:String = key+"&"+subKey;
			var returnObj:*;
			if(_classDic[key]){
				returnObj=(_classDic[key] as BitmapData).clone();
			}
			/*if(callBackFun!=null){
				if(returnObj){
					callBackFun(returnObj,classkey);
				}else{
					if(!_callBackDic[key])_callBackDic[key] =  new Array();
					var index:int = _callBackDic[key].indexOf(callBackFun);
					if(index !=-1){
						_callBackDic[key].splice(index,1,callBackFun);
					}else _callBackDic[key].push(callBackFun);
				}
			}*/
			if(callBackFun!=null){
				if(returnObj){
					callBackFun(returnObj,classkey);
				}else{
					if(!_callBackDic[key])_callBackDic[key] =  new Array();
					var index:int = _callBackDic[key].indexOf(callBackFun);
					if(index==-1)_callBackDic[key].push(callBackFun);
				}
			}
			return returnObj;
		}
		
		/**
		 *回调 
		 * @param key
		 * 
		 */
		private function callBack(key:String):void{
			if(_callBackDic[key]){
				var callList:Array = _callBackDic[key] as Array;
				var count:int = callList.length;
				/*for(var i:int=0;i<count;i++){
					var func:Function = callList[i];
					getData(key,"",func);
				}*/
				for(var i:int=count-1;i>-1;i--){
					var func:Function = callList[i];
					if(func!=null)
						getData(key,"",func);
					callList.splice(i,1);
				}
				delete _callBackDic[key];
			}
		}
		
		/**
		 *设置数据 
		 * @param item
		 * 
		 */
		public function setData(item:LoadingItem):void{
			var key:String = item.id?item.id:item.url.url;
			if(item as ImageItem && (item as ImageItem).content as Bitmap){
//				((item as ImageItem).content as Bitmap).smoothing = true;
				_classDic[key] = ((item as ImageItem).content as Bitmap).bitmapData.clone();
			}
			item.destroy();
			item = null;
			callBack(key);
		}
	}
}